﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using PPIDesktopClient.Logic;

namespace PPIDesktopClient.Presenter
{
    public interface IDownloadStats
    {
        bool UtilityCheck{ get;}
        bool VendorCheck{ get;}
        int ProgressBarValue{ set;}
        bool IsFinish{ set;}
        event EventHandler<EventArgs> StartDownload;
        event EventHandler<EventArgs> StopDownload;
        int TotalImagesDesktop{ set;}
        int TotalImagesToDownload{ set;}
        event EventHandler<EventArgs> OnLoad;

    }


    public class DownloadStatsPresenter
    {
        private IDownloadStats m_View;
        private DownloadImageProcess m_DownloadProcess;
        private int m_TotalImages;
        private int m_UploadedImages;
        private Stack<EBaseImage> m_StackImages;
        private bool m_IsCancelled;
        private EBaseImage m_TempImageToDownload;
        private XMLHelperDownloadImage m_XMLManager;

        public DownloadStatsPresenter(IDownloadStats view)
        {
            m_View = view;
            m_TotalImages = 0;
            m_UploadedImages = 0;
            m_StackImages = new Stack<EBaseImage>();
            m_DownloadProcess = new DownloadImageProcess();
            m_DownloadProcess.FinishDownloadImage += new EventHandler(m_DownloadProcess_FinishDownloadImage);
            m_IsCancelled = false;
            Initialize();
        }
        private void Initialize()
        {
            m_View.StartDownload += new EventHandler<EventArgs>(m_View_StartDownload);
            m_View.StopDownload += new EventHandler<EventArgs>(m_View_StopDownload);
            m_View.OnLoad += new EventHandler<EventArgs>(m_View_OnLoad);
        }

        void m_View_OnLoad(object sender, EventArgs e)
        {
            OnLoad();
        }

        void m_View_StopDownload(object sender, EventArgs e)
        {
            m_IsCancelled = true;
        }

        void m_View_StartDownload(object sender, EventArgs e)
        {
            StartDownload();
        }

        void m_DownloadProcess_FinishDownloadImage(object sender, EventArgs e)
        {
            try
            {
                if (m_TempImageToDownload.GetLocalPath().Exists)
                {
                    m_XMLManager.AddImageNode(m_TempImageToDownload.Asset.Id, m_TempImageToDownload.Id, m_TempImageToDownload.GetLocalPath().Name, m_TempImageToDownload.GetLocalPath().FullName);
                    m_XMLManager.Save();
                }
                m_UploadedImages++;
                int tempProgress = (m_UploadedImages * 100) / m_TotalImages;
                m_View.ProgressBarValue = tempProgress;
                DownloadImage();
            }
            catch (Exception ex)
            {
                ErrorManager.Log("DownloadStatsPresenter.m_DownloadProcess_FinishDownloadImage()", ex.Source, ex.Message, ex.StackTrace);
            }
        }

        public void StartDownload()
        {

            try
            {
                m_IsCancelled = false;
                PrepareForDownload();
                DownloadImage();

            }
            catch (Exception ex)
            {
                ErrorManager.Log("DownloadStatsPresenter.StartDownload()", ex.Source, ex.Message, ex.StackTrace);
            }
        }

        private void PrepareForDownload()
        {
            string _xmlPath;
            EBaseImage _ImageManager;
            if (m_View.UtilityCheck)
            {
                _xmlPath = Constants.k_XmlImageUtilityPath;
                _ImageManager = new EUtility_Asset_XMissionStructures_Image();
            }
            else
            {
                _xmlPath = Constants.k_XmlImagePath;
                _ImageManager = new EVendor_Visit_XMissionStructures_Image();
            }
            m_XMLManager = new XMLHelperDownloadImage(_xmlPath);
            int m_LastOffSet = m_XMLManager.GetLastImageID();
            m_StackImages = new Stack<EBaseImage>(_ImageManager.GetServer(m_LastOffSet));
            m_TotalImages = m_StackImages.Count;
            m_View.TotalImagesToDownload = m_TotalImages;
        }

        private void DownloadImage()
        {
            try
            {
                if (m_StackImages.Count > 0 && !m_IsCancelled)
                {
                    m_TempImageToDownload = m_StackImages.Pop();
                    m_DownloadProcess.StartDownload(m_TempImageToDownload);
                }
                else
                {
                    m_View.IsFinish = true;
                }
            }
            catch (Exception ex)
            {
                ErrorManager.Log("DownloadStatsPresenter.DownloadImage()", ex.Source, ex.Message, ex.StackTrace);
            }
        }

        private void OnLoad()
        {
            m_View.TotalImagesDesktop =
                LVendor_Visit_XMissionStructures_Image.CountImagesInDesktop(Constants.K_ImageCachePath);
        }
    }
}
